package org.glut.AppStoreProject.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.glut.AppStoreProject.entity.RankingList;
import org.glut.AppStoreProject.service.RankingListServiceImapl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = {"/rankinglist"})
public class RankingListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        List<RankingList> respList = new RankingListServiceImapl().getRankingList();
        //使用 Gson 将列表数据序列化成 json 字符串
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .setPrettyPrinting() //设置json 有缩进格式
                .create();
        String json = gson.toJson(respList);
        //将json 字符串写回前端
        writer.write(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
